package com.fstech.whiteboadrd.server;


import android.graphics.Bitmap;

import com.allen.library.RxHttpUtils;
import com.allen.library.interceptor.Transformer;
import com.allen.library.observer.CommonObserver;
import com.fstech.whiteboadrd.server.api.WhitePadApi;
import com.fstech.whiteboadrd.server.response.BaseResponse;

import java.io.ByteArrayOutputStream;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;

public class ServerUtil {

    public static void share(Bitmap img, CommonObserver<BaseResponse<String>> callback) {
        RequestBody body = RequestBody.create(MediaType.parse("multipart/form-data"), getBytes(img));
        RxHttpUtils.createApi(WhitePadApi.class)
                .uploadImg(body)
                .compose(Transformer.<BaseResponse<String>>switchSchedulers())
                .subscribe(callback);
    }

    private static byte[] getBytes(Bitmap bitmap) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        byte[] datas = baos.toByteArray();
        return datas;
    }

}
